﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class Masters : ClassManager.SessionManagerPage
    {
        private void GetAllMasters()
        {
            MastersGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.MastersProvider.GetAll();
        }

        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = GPSTrackingDBV2.Entities.MastersColumn.MasterUsername.ToString();
                expression.SetSortOrder("Ascending");
                this.MastersGrid.MasterTableView.SortExpressions.AddSortExpression(expression);
            }
        }

        protected void MastersGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GetAllMasters();

            MastersGrid.Height = Unit.Parse(HiddenFieldHeight.Value.ToString());
        }

        protected void MastersGrid_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {                
                GPSTrackingDBV2.Entities.Masters MasterEntity = ClassManager.InvokeEntityManager.Invoke_Masters_Entity(e);
                MasterEntity.Password = EncryptaionManager.EncryptionManager.Encrypt(MasterEntity.Password, new System.Security.Cryptography.SHA256CryptoServiceProvider());
                MasterEntity.CreationDate = DateTime.Now;

                GPSTrackingDBV2.Data.DataRepository.MastersProvider.Insert(MasterEntity);
            }
            catch (Exception ex)
            {                
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede insertar la información.");
            }
        }

        protected void MastersGrid_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                GPSTrackingDBV2.Entities.Masters MasterEntity = ClassManager.InvokeEntityManager.Invoke_Masters_Entity(e);

                MasterEntity.OriginalMasterUsername = MasterEntity.MasterUsername;

                Telerik.Web.UI.GridEditableItem item = e.Item as Telerik.Web.UI.GridEditableItem;
                MasterEntity.Password = item.SavedOldValues["Password"].ToString();

                GPSTrackingDBV2.Data.DataRepository.MastersProvider.Update(MasterEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede modificar la información.");
            }
        }

        protected void MastersGrid_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                GPSTrackingDBV2.Entities.Masters MasterEntity = ClassManager.InvokeEntityManager.Invoke_Masters_Entity(e);

                GPSTrackingDBV2.Data.DataRepository.MastersProvider.Delete(MasterEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede eliminar la información.");
            }
        }

        protected void MastersGrid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.OwnerTableView.IsItemInserted && e.Item.IsInEditMode) //item is about to be inserted  
            {
                TextBox txtid;
                GridEditableItem insertItem = (GridEditableItem)e.Item;
                txtid = (TextBox)insertItem["Password"].Controls[0];
                txtid.ReadOnly = false;
                txtid.TextMode = TextBoxMode.Password;
                txtid = (TextBox)insertItem["MasterUsername"].Controls[0];
                txtid.ReadOnly = false;
            }
            if (!(e.Item.OwnerTableView.IsItemInserted) && e.Item.IsInEditMode)   //item is about to be edit
            {
                TextBox txtid;
                GridEditableItem editItem = (GridEditableItem)e.Item;
                txtid = (TextBox)editItem["Password"].Controls[0];
                txtid.TextMode = TextBoxMode.Password;
                txtid.ReadOnly = true;
                txtid = (TextBox)editItem["MasterUsername"].Controls[0];
                txtid.ReadOnly = true;
            }  
        }

        protected void lbClearFilters_Click(object sender, EventArgs e)
        {
            MastersGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in MastersGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
            }


            MastersGrid.MasterTableView.Rebind();
        }

        public void showErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_miniError", "objWindowsMessageManager.show_miniError('" + msg + "','Master ...');", true);
        }

        protected void lbShowError_Click(object sender, EventArgs e)
        {
            showErrorMessage(HiddenFieldError.Value);
        }
    }
}